$(function(){
	var $Ts1 = $("#TS1");
	var $Ts4 = $("#TS4");
	var username = 	$("#username");
	$("#username").val("");
	$("#yzm_input").val("");
	username.focus(function(){
		$Ts1.text("请输入您的手机号");
		$Ts1.removeClass("Ts1error");
	});
	var usernameOk = false;
	var notHaveUser = true;
	username.focus(function(){
		notHaveUser = true;
	})
	username.blur(function(){
		if(/^1[35678][0-9]{9}$/.test($(this).val())){
			// 验证该号码是否已经被注册
			var telephone = $("#username").val();
			$.ajax({
				url:"MemberServlet",
                dataType:"json",
                data:{
                	methodName:"isRegister",
                    telephone:telephone
                },
                success:function(data){
                	// var data1 = $.parseJSON(data); 
                	if(data.result=="SUCCESS"){
                		notHaveUser = false;
                		$Ts1.text("账号已存在！");
        				$Ts1.addClass("Ts1error");
                	} else{
                		changecheckcode();
        				$("#yzm_box").show(200);
        				$Ts1.text("");
        				$(this).removeClass("input_error");
        				$Ts1.removeClass("Ts1error");
        				usernameOk = true;
                	}
                }
			});
		}else{
			$("#yzm_box").hide(200);
			$(this).addClass("input_error");
			$Ts1.text("请输入正确的手机号！");
			$Ts1.addClass("Ts1error");
			usernameOk = false;
		}
	});
	
	// 动态图片验证码
	var yzm = {
		"w8dp":"img/imgszcdl/yzm/yzm1.png",
		"nrwc":"img/imgszcdl/yzm/yzm2.png",
		"ncni":"img/imgszcdl/yzm/yzm3.png",
		"xaph":"img/imgszcdl/yzm/yzm4.png",
		"jtdy":"img/imgszcdl/yzm/yzm5.png"
		}
	var last;
	var yzm_index = ["w8dp","nrwc","ncni","xaph","jtdy"];
	
	// 随机切换动态图片验证码
	function changecheckcode(){
		var index = Math.round(Math.random()*5);
		if(index == last){
			changecheckcode();
		}else{
			last = index;
			$("#checkcode").attr('src',yzm[yzm_index[index]]);
		}
	}
	
	changecheckcode();
	$("#checkcode,#changeyzm").on("click",function(){
		changecheckcode();
	});
	
	var okclick = true;
	var yzmOk = false;
	
	// 动态图片验证码离焦事件触发
	$("#yzm_input").blur(function(){
		if(yzm_index[last] == $("#yzm_input").val().toLowerCase()){
			yzmOk = true;
			$Ts1.text("");
		}else{
			yzmOk = false;
			$Ts1.text("图形验证码错误!");
			$(this).addClass("input_error");
			$Ts1.addClass("Ts1error");
		}
	});
	
	// 获取手机验证码点击事件
	$("#sendMsg").click(function(){
		if(okclick){
			if(yzmOk){
				okclick = false;
				sendMsg();
				//$Ts1.text("手机验证码发送成功!");
				//$Ts1.addClass("Ts1error");
				var time = 60;
				var t1 = setInterval(function(){
					time--;
					$("#sendMsg").text(time+"s后重新发送");
					if(time == 0){
						clearInterval(t1);
						$("#sendMsg").text("发送手机验证码");
						okclick = true;
					}
				},1000);
			}
		}
	});
	
	// 获取手机验证码
	var msgyzm = 0;
	/*function sendMsg(){
		msgyzm = Math.round(Math.random()*8999)+1000;
		console.log(msgyzm);
		var _url = "http://106.ihuyi.com/webservice/sms.php?method=Submit&account=cf_390010249&password=aef571b45421fce92f5c365ed8ab8db3&content=您的验证码是："+ msgyzm +"。请不要把验证码泄露给其他人。";
		$.ajax({
			url:_url,
			dataType:"jsonp",
			data:{mobile:$("#username").val()},
		});
	}*/
	
	function sendMsg(){
		$.ajax({
			url:"MemberServlet",
			dataType:"json",
			data:{
				methodName:"getSmsCode",
				telphone:$("#username").val()
			},
			success:function(data){
             	if(data.result=="SUCCESS"){
             		// 发送成功
             		$Ts1.text(data.message);
    				$Ts1.addClass("Ts1error");
             	} else{
             		// 发送失败
             		$Ts1.text(data.message);
    				$Ts1.addClass("Ts1error");
             	}
             }
		});
	}
	
	// 输入密码离焦事件
	var $Ts2 = $("#TS2");
	var passwordOk = false;
	var dxxz = false; // 产生的验证码和用户输入的验证码是否一致
	$("#password").blur(function(){
		if(/^.{6,18}$/.test($(this).val())){
			$("#TS2").text("");
			$(this).removeClass("input_error");
			$Ts2.removeClass("Ts1error");
			passwordOk = true;
		}else{
			$("#TS2").text("密码的长度为6-18位!");
			$(this).addClass("input_error");
			$Ts2.addClass("Ts1error");
			passwordOk = false;
		}
	});
	
	// 输入短信验证码验证码聚焦事件
	$("#msgyzm_input").focus(function(){
		$("#TS1").text("");
		dxxz = false;
	})
	
	// 输入短信验证码离焦事件，去验证用户输入的短信验证码和后台产生的验证码是否一致
	$("#msgyzm_input").blur(function(){
		/*if(msgyzm != $("#msgyzm_input").val()){
			$("#TS1").text("短信验证码错误!");
		}else{
			dxxz = true;
		}*/
		$.ajax({
			url:"MemberServlet",
			dataType:"json",
			data:{
				methodName:"checkSmsCode",
				inputSmsCode:$("#msgyzm_input").val()
			},
			success:function(data){
             	if(data.result=="SUCCESS"){
             		// 发送成功
             		$Ts1.text(data.messge);
    				$Ts1.addClass("Ts1error");
             	} else{
             		// 发送失败
             		$Ts1.text(data.message);
    				$Ts1.addClass("Ts1error");
             	}
             }
		});
	})
	
	// 再次输入密码离焦事件
	var $Ts3 = $("#TS3");
	var passwordTwoOk = false;
	$("#passwordTwo").blur(function(){
		if($("#password").val()==$(this).val()){
			$("#TS3").text("");
			$(this).removeClass("input_error");
			$Ts3.removeClass("Ts1error");
			passwordTwoOk = true;
		}else{
			$("#TS3").text("两次输入的不一致，请重新输入");
			$(this).addClass("input_error");
			$Ts3.addClass("Ts1error");
			passwordTwoOk = false;
		}
	});
	
	// 二货邀请码，选中则显示输入框
	function yqmcheck(){
		if($("#yqm").prop("checked")){
			$("#yqm_input").show();
		}else{
			$("#yqm_input").hide();
		}
	}
	yqmcheck();
	
	// 二货邀请码点击事件
	$("#yqm").click(function(){
		yqmcheck();
	});
	// 注册点击事件
	$("#reg").click(function(){
		if($("#yzm_input").val()==""){
			$Ts1.text("请输入验证码!");
			$("#username").addClass("input_error");
			$Ts1.addClass("Ts1error");
		}
		if($("#username").val()==""){
			$Ts1.text("请输入用户名!");
			$("#username").addClass("input_error");
			$Ts1.addClass("Ts1error");
		}
		if($("#password").val()==""){
			$Ts2.text("请输入密码!");
			$("#password").addClass("input_error");
			$Ts2.addClass("Ts1error");
		}
		if($("#passwordTwo").val()==""){
			$Ts3.text("请再次输入密码!");
			$("#passwordTwo").addClass("input_error");
			$Ts3.addClass("Ts1error");
		}
		if($("#s_province").val()==""){
			$Ts4.text("请选择省份!");
			$("#s_province").addClass("input_error");
			$Ts4.addClass("Ts1error");
		}
		if($("#s_city").val()==""){
			$Ts4.text("请选择城市!");
			$("#s_city").addClass("input_error");
			$Ts4.addClass("Ts1error");
		}
		if($("#s_school").val()==""){
			$Ts4.text("请选择高校!");
			$("#s_school").addClass("input_error");
			$Ts4.addClass("Ts1error");
		}
		
		if(usernameOk&&passwordOk&&passwordTwoOk&&yzmOk&&notHaveUser&&dxxz){
			$("#registerform").submit();
		}
	});
	
	// 注册成功倒计时跳转到登陆页面
	//提示框面向对象封装 var s = new Toast("显示内容",保留时间); s.show();
	function Toast(val,overtime,callback){
		var self = this;
		this.val = val;
		this.overtime = overtime || 2000;
		this.init = function(){
			this.toast = document.createElement("div");
			this.toast.innerHTML = this.val;
			with (this.toast.style){
				padding = "15px 25px";
				position="fixed";
			 	left="50%";
				background="#666";
				color="#fff";
				borderRadius="5px";
				top="50%";
				width="200px";
				textAlign="center";
			}
			
		}
		this.show = function(){
			this.init();
			document.body.appendChild(this.toast);
			this.toast.style.marginLeft = -this.toast.offsetWidth/2 + "px";
			this.toast.style.marginTop = -this.toast.offsetHeight/2 + "px";
			this.wait();
		}
		this.wait = function(){
			setTimeout(function() {
				var op = 1;
				var t1 = setInterval(function() {
					op -= 0.1;
					self.toast.style.opacity = op;
					if(op <= 0) {
						clearInterval(t1);
						self.toast.remove();
						callback?callback():"";
					}
				}, 50);
		
			}, this.overtime);
		}
		
	}
	
	var telphone = $("#username").val();
	var password = $("#password").val();
	var province = $("#s_province").val();
	var city = $("#s_city").val();
	var school = $("#s_school").val();
	// 提交注册表单
	$("#registerform").form({
		url:"MemberServlet",
		/*dataType:"json",
        data:{
        	methodName:"isRegister",
        	telphone:telphone,
        	password:password,
        	province:province,
        	city:city,
        	school:school
        },*/
		onSubmit: function() {},
		success: function(data) {
			//var data1 =$.parseJSON(data);
        	if(data.result=="SUCCESS"){
            	var miao = 3;
         		var dao = setInterval(function(){
         			var s = new Toast("注册成功,"+ miao +"秒后跳转登录页面"); 
         			s.show();
         			miao--;
         			if(miao == -1){
         				clearInterval(dao);
         			}
         		},1000)
         		setTimeout(function(){
         			location.href = "MemberServlet?methodName=gotoIndex";
         		},4000);
             } else {
            	 
             }
         }
	});
	
	// 页面加载初始化省份列表
	var i;
	$(".option1").remove();
	$.ajax({
		url:"ProvinceServlet",
		type:"get",
		dataType:"json",
		data:{
				"methodName":"selectAllProvince"
			},
		success:function(data){
			for(i=0;i<data.data.length;i++){
				$("#s_province").append("<option class='option1' value='"+data.data[i].id+"'>"+data.data[i].name+"</option>")
			}
		}
	});
	
	// 省份改变，获取城市列表
	$("#s_province").on("change", function(){
		var provinceId = $("#s_province :checked").val();
		$(".option2").remove();
		$.ajax({
			url:"CityServlet",
			type:"get",
			dataType:"json",
			data:{
					"methodName":"selectByProvinceId",
					provinceId:provinceId
				},
			success:function(data){
				for(i=0;i<data.data.length;i++){
					$("#s_city").append("<option class='option2' value='"+data.data[i].id+"'>"+data.data[i].name+"</option>")
				}
			}
		});
	});
	
	// 城市改变，获取高校列表
	$("#s_city").on("change", function(){
		var cityId = $("#s_city :checked").val();
		$(".option3").remove();
		$.ajax({
			url:"SchoolServlet",
			type:"get",
			dataType:"json",
			data:{
					"methodName":"selectByCityId",
					cityId:cityId
				},
			success:function(data){
				//var data1 =$.parseJSON(data);
				for(i=0;i<data.data.length;i++){
					$("#s_school").append("<option class='option3' value='"+data.data[i].id+"'>"+data.data[i].name+"</option>")
				}
			}
		});
	});
});